package top.codecrab.eblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.codecrab.eblog.common.response.R;
import top.codecrab.eblog.entity.Post;
import top.codecrab.eblog.entity.User;
import top.codecrab.eblog.search.mq.MqTypes;
import top.codecrab.eblog.shiro.AccountProfile;

/**
 * @author codecrab
 */
@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    /**
     * 管理员操作之 加精，置顶，删除
     *
     * @param id    文章id
     * @param field 方法的属性，具体为上面三种的哪一种
     * @param rank  1表示操作，0表示取消
     */
    @ResponseBody
    @PostMapping("/jie-set")
    public R jieSet(Long id, String field, Integer rank) {
        return adminService.jieSet(id, field, rank);
    }

    /**
     * 管理员初始化es
     */
    @ResponseBody
    @PostMapping("/initEsData")
    public R initEsData() {
        long initTotal = searchService.initES();
        return R.ok("ES初始化成功，共有 " + initTotal + " 条数据", initTotal);
    }

    @GetMapping("/check")
    public String check() {
        return "admin/checkPost";
    }

    @ResponseBody
    @PostMapping("/check/noCheck")
    public R doCheck() {
        IPage<Post> page = postService.page(
            getPage(),
            new QueryWrapper<Post>()
                .eq("status", 0)
        );
        return R.ok(page);
    }

    @GetMapping("/pass")
    public String pass(Long id) {
        Post post = postService.getById(id);
        post.setStatus(1);
        postService.updateById(post);
        return "admin/checkPost";
    }

    @GetMapping("/delete")
    public String delete(Long id) {
        postService.delete(id);
        return "admin/checkPost";
    }

    @GetMapping("/user")
    public String userList() {
        IPage<User> iPage = userService.page(getPage());
        request.setAttribute("pageData", iPage);
        return "admin/userSet";
    }

    @ResponseBody
    @GetMapping("/user/{userId}")
    public R userInfo(@PathVariable Long userId) {
        User user = userService.getById(userId);
        return R.ok(user);
    }

    @ResponseBody
    @PostMapping("/user/set")
    public R doSet(AccountProfile user, String province) {
        user.setCity(province + " " + user.getCity());
        return userService.adminDoSet(user);
    }

    /**
     * 修改用户的状态
     *
     * @param userId 用户的ID
     * @param status -2：封禁，0解封
     */
    @ResponseBody
    @PostMapping("/user/status/{userId}/{status}")
    public R updateUserStatus(@PathVariable Long userId, @PathVariable Integer status) {
        boolean update = userService.update(
            new UpdateWrapper<User>()
                .eq("id", userId)
                .set("status", status)
        );

        return update ? R.ok() : R.fail("修改账户状态出现意外");
    }
}
